var mongoose=require('mongoose');
var Schema=mongoose.Schema;
var config=require('../config');
var Active=require('./avtive')
var User1=require('./user')
var db=mongoose.createConnection(config.db_url);
db.on('error',function(error){
    console.log(error);
});
db.model('user',User1.userSchema);
db.model('active',Active.activeSchema);
var loveActiveSchema=new Schema({
    user:{type:Schema.Types.ObjectId,ref:'user'},
    date:{type:Date,default:Date.now},
    active:{type:Schema.Types.ObjectId,ref:'active'}
});
//users---对应数据库表名
var LoveActive=db.model('LoveActive',loveActiveSchema);



//------------------------------------------------------------------
//分组查询：
LoveActive.prototype.findLoveActiveGroup = function (options,callback) {
    //count这里用了$sum:1实现http://docs.mongodb.org/manual/reference/operator/aggregation/group/
    LoveActive.aggregate()
        .match(options)      //条件
        .group({_id: '$user', count: {$sum:1}})//count:指定一个字段，字段名可随意，1表示所有记录的count字段值为1,$sum在求和
        .sort('-count')                        //数组结果降序
        .project('_id')      //1表示字段返回，0：不返回
        .limit(8)                              //过滤条数
        .exec(function (err, groupDatas) {

            callback(err, groupDatas);
        });

}

//------------------------------------------------------------------------
LoveActive.prototype.findAllLoveActive=function(callback){
    LoveActive.find({},function(err,datas){
        callback(err,datas);
    })
}

LoveActive.prototype.updateLoveActiveById=function(id,options,callback){
    LoveActive.update({_id:id},options,function(err){
        callback(err)
    });
}
LoveActive.prototype.updateLoveActiveByOpt=function(opt,options,callback){
    LoveActive.update(opt,options,{ multi: true },function(err){       //multi:批量操作
        callback(err)
    });
}
LoveActive.prototype.findLoveActiveById=function(id,callback){
    LoveActive.findOne({_id:id},function(err,data){
        callback(err,data);
    });
}

LoveActive.prototype.findLoveActiveByOptions=function(options,callback){
    LoveActive.find(options,function(err,datas){
        callback(err,datas);
    });
}
LoveActive.prototype.delLoveActiveById=function(id,callback){
    LoveActive.remove({_id:id},function(err){
        callback(err);
    })
}

LoveActive.prototype.findForPage=function(obj,callback){
    var q=obj.search||{};                                       //查询条件
    var col=obj.columns||{};                                    //返回字段
    var query=LoveActive.find(q,col).skip(obj.page.skipFrom).limit(obj.page.limit).sort('-date').populate('active');
    query.exec(function(err, datas) {
        if (err) {
            callback(err, null, null);
        } else {
            LoveActive.count(q, function(err, count) {
                if (err) {
                    callback(err, null, null);
                } else {
                    var pageCount = Math.ceil(count / obj.page.limit);
                    callback(null, count,pageCount, datas);
                }
            });
        }
    });

}
module.exports =LoveActive;


